package cn.anecansaitin.not_enough_harness.common.harness_data;

import cn.anecansaitin.not_enough_harness.common.item.ModComponents;
import com.mojang.serialization.Codec;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;

public record RushTriggerHarnessDirection(Vec3 direction) {
    public static final Codec<RushTriggerHarnessDirection> CODEC = Vec3.CODEC.xmap(
            RushTriggerHarnessDirection::new,
            RushTriggerHarnessDirection::direction
    );

    public static final StreamCodec<ByteBuf, RushTriggerHarnessDirection> STREAM_CODEC =
            Vec3.STREAM_CODEC.map(
                    RushTriggerHarnessDirection::new,
                    RushTriggerHarnessDirection::direction
            );

    public static RushTriggerHarnessDirection get(ItemStack item) {
        RushTriggerHarnessDirection rushTriggerHarnessDirection = item.get(ModComponents.RUSH_TRIGGER_HARNESS_DIRECTION);

        if (rushTriggerHarnessDirection == null) {
            rushTriggerHarnessDirection = new RushTriggerHarnessDirection(new Vec3(0, 0, 0));
            item.set(ModComponents.RUSH_TRIGGER_HARNESS_DIRECTION, rushTriggerHarnessDirection);
        }

        return rushTriggerHarnessDirection;
    }

    public static void set(ItemStack item, Vec3 direction) {
        item.set(ModComponents.RUSH_TRIGGER_HARNESS_DIRECTION, new RushTriggerHarnessDirection(direction));
    }
}
